#include "mt3d_cuda.h"

void mt3d::apply_boundary_condition()
{
    std::clog << "Applying boundary condition ... ";

    // 采用划0置1的方法处理第一类边界条件 （稀疏矩阵处理起来很快）

    for (size_t i = 0; i < edge_num_; ++i)
    {
        if (edge_on_boundary_[i])
        {
            h_B_TE_[i] = _zero; // 二次场方法计算中假设边界上的二次场已经衰减为0
            h_B_TM_[i] = _zero;
        }
    }

    int r, c;
    for (size_t i = 0; i < nz_num_; i++)
    {
        r = h_rowIdxA_[i]; c = h_colIdxA_[i];
        if (!edge_on_boundary_[r] && edge_on_boundary_[c])
        {
            h_B_TE_[r] = clcg_Zdiff(h_B_TE_[r], cuCmul(h_A_[i], h_B_TE_[c]));
            h_B_TM_[r] = clcg_Zdiff(h_B_TM_[r], cuCmul(h_A_[i], h_B_TM_[c]));
        }
    }

    for (size_t i = 0; i < nz_num_; i++)
    {
        r = h_rowIdxA_[i]; c = h_colIdxA_[i];
        if (edge_on_boundary_[r] || edge_on_boundary_[c])
        {
            if (r == c) h_A_[i] = _one;
            else h_A_[i] = _zero;
        }
    }

    std::clog << "done\n";
    return;
}
